home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / fstream.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  1KB  |  60 lines

  1.                        // Chapter 1 - Program 4
  2. #include <iostream.h>
  3. #include <fstream.h>
  4. #include <process.h>
  5.  
  6. void main()
  7. {
  8. ifstream infile;
  9. ofstream outfile;
  10. ofstream printer;
  11. char filename[20];
  12.  
  13.    cout << "Enter the desired file to copy ----> ";
  14.  
  15.    cin >> filename;
  16.  
  17.    infile.open(filename, ios::nocreate);
  18.    if (!infile) {
  19.       cout << "Input file cannot be opened.\n";
  20.       exit(1);
  21.    }
  22.  
  23.    outfile.open("copy");
  24.    if (!outfile) {
  25.       cout << "Output file cannot be opened.\n";
  26.       exit(1);
  27.    }
  28.  
  29.    printer.open("PRN");
  30.    if (!printer) {
  31.       cout << "There is a problem with the printer.\n";
  32.       exit(1);
  33.    }
  34.  
  35.    cout << "All three files have been opened.\n";
  36.  
  37. char one_char;
  38.  
  39.    printer << "This is the beginning of the printed copy.\n\n";
  40.  
  41.    while (infile.get(one_char)) {
  42.       outfile.put(one_char);
  43.       printer.put(one_char);
  44.    }
  45.  
  46.    printer << "\n\nThis is the end of the printed copy.\n";
  47.  
  48.    infile.close();
  49.    outfile.close();
  50.    printer.close();
  51.  
  52. }
  53.  
  54.  
  55.  
  56. // Result of execution
  57. //
  58. // (The input file is copied to the file named "COPY")
  59. // (The input file is printed on the printer
  60.